package com.zhan.signalling.store.tablestore.message;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import com.alicloud.openservices.tablestore.timeline.common.TimelineException;
import com.alicloud.openservices.tablestore.timeline.common.TimelineExceptionType;
import com.alicloud.openservices.tablestore.timeline.message.IMessage;
import com.alicloud.openservices.tablestore.timeline.utils.Utils;

public class WhiteBoardMessage implements IMessage {
	
	
	private static AtomicInteger baseID = new AtomicInteger(0);
	
	private static String machineID = Utils.getProcessID() + "@" + Utils.getLocalIP() + ":";
	
	private String messageID = null;
	
	private Map<String, String> attributes;

	public WhiteBoardMessage() {
		this.attributes = new HashMap<>();
	}
	
	public WhiteBoardMessage(Map<String, String> attributes) {
		super();
		this.attributes = attributes;
	}



	@Override
	public String getMessageID() {
		if(messageID == null) {
			baseID.compareAndSet(Integer.MAX_VALUE, 0);
			messageID = machineID + String.valueOf(baseID.addAndGet(1));
		}
		return String.valueOf(messageID);
	}

	@Override
	public void setMessageID(String messageID) {
		if(messageID == null || messageID.isEmpty()) {
			throw new TimelineException(TimelineExceptionType.INVALID_USE, "message id is null or empty");
		}

	}

	@Override
	public IMessage newInstance() {
		
		return new WhiteBoardMessage();
	}

	@Override
	public byte[] serialize() {
		
		return new byte[0];
	}

	@Override
	public void deserialize(byte[] input) {
		
	}

	@Override
	public void addAttribute(String name, String value) {
		attributes.put(name, value);

	}

	@Override
	public void updateAttribute(String name, String newValue) {
		addAttribute(name, newValue);

	}

	@Override
	public Map<String, String> getAttributes() {
		return attributes;
	}


}
